package com.sinodata.bsm.cicp.finder.oracle;

import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;

import com.sinodata.bsm.cicp.finder.Finder;
import com.sinodata.bsm.cicp.util.DBConnectionUtil;

/**
 * 
 * <p>
 * Description: Oracle口令状态发现程序
 * 程序返回：用户名，创建时间
 * </p>
 *
 * @author wangpeng
 * @version 1.0

 * <p>
 * History: 
 *
 * Date                     Author         Version     Description
 * ---------------------------------------------------------------------------------
 * 2012-6-11 下午02:41:31          wangpeng        1.0         To create
 * </p>
 *
 * @since 
 * @see
 */
public class OracleFinder extends Finder {

    private static final Logger logger = Logger.getLogger(OracleFinder.class);

    @SuppressWarnings("unchecked")
    @Override
    public List<String[]> find(String ip, Map params) {
        List<String[]> list = new ArrayList<String[]>();
        String sql = "select username,to_char(created,'yyyy-mm-dd hh24:mi') from sys.dba_users Order By  upper(username)";
        String serverName = (String) params.get("SERVERNAME");
        Connection conn = null;
        Statement stat = null;
        ResultSet rs = null;
        try {
            conn = DBConnectionUtil.getInstance().getConnection("oracle", ip, serverName);
            stat = conn.createStatement();
            rs = stat.executeQuery(sql);
            int i = 0;
            while (rs.next()) {
                String[] userInfo = new String[5];
                list.add(userInfo);
                userInfo[0] = i + "Oracle_user";
                userInfo[1] = rs.getString(2);
                userInfo[2] = rs.getString(2);
                userInfo[3] = rs.getString(1); //用户名
                userInfo[4] = rs.getString(2); //创建时间
                i++;
            }
        } catch (SQLException e) {
            logger.error("failed to execyte sql", e);
        } catch (IOException e) {
            logger.error("failed to reader a writer", e);
        } finally {
            if (stat != null) {
                try {
                    stat.close();
                } catch (Exception e) {
                    logger.error("failed to close statement", e);
                }
            }
            DBConnectionUtil.getInstance().returnConnection(conn);//归还连接
        }
        return list;
    }

}
